package net.nshc.kwkim.mycalc.buttonlistener;

import net.nshc.kwkim.mycalc.*;
import net.nshc.kwkim.mycalc.activity.*;
import net.nshc.kwkim.mycalc.util.*;
import android.content.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public class EqualSignButtonClickListener extends CalcButtonClickListener {

	public EqualSignButtonClickListener(Context context) {
		super(context);
	}

	public EqualSignButtonClickListener(Context context, boolean abLocation) {
		super(context,abLocation);
	}

	@Override
	public void onClick(View v) {
		resultString(editTextInput, TextViewDisplay);
	}

	@Override
	protected void resultString(EditText editText, final TextView TextViewdisplay) {
		//		final ScrollView scroll = (ScrollView)((Activity)context).findViewById(R.id.scroll);
		int RBlock = 0, LBlock = 0;
		StringBuilder textFromEditText = new StringBuilder(ButtonContentAnalyzer.hasStingErrorMessage(editText.getText().toString().trim()));
		if(textFromEditText.length()==0){
			editText.setText(textFromEditText);
			return;
		}
		PostFixExp post = new PostFixExp();
		String Value = new String();
		String Record = new String();
		Double Result = null;

		if (textFromEditText != null) {
			Value = changeExtendexp(textFromEditText.toString());
			RBlock = countRBracket(Value);
			LBlock = countLBracket(Value);
			
			if((LBlock-RBlock) < 0 || DisplayContentAnalyzer.isTheLastCharacterArithmeticOperator(Value) || !DisplayContentAnalyzer.isOperator(Value)){
				if(Value.equals(""))return;
				if(!DisplayContentAnalyzer.isOperator(Value))return;
				if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "EqualBtn_log_LBlock-RBlock = "+(LBlock-RBlock));
				Record = textFromEditText+"=error\n";
				MyCalcActivity.collectionData(Record, super.bLocation);
				TextViewdisplay.append(Record);
				TextViewdisplay.invalidate();
				editText.setText("error");
				
			}else if((LBlock-RBlock) > 0){
				
				if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "EqualBtn_log_(LBlock-RBlock) = "+(LBlock-RBlock));
				
				for(int i = 0; i <(LBlock-RBlock);i++){
					Value = Value.concat(")");
					textFromEditText.append(")");
					
					if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "(LBlock-RBlock)_EqualBtn_log_for = "+Value);
					
				}
				
				if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "EqualBtn_log_Value = "+Value);
				
				Value  = post.infixToPostfix(Value);
				Result = post.result(Value);
				
				if(MyCalcActivity.bLog)	Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "EqualBtn_log_postfix_Value = "+Value);
				
				Record = textFromEditText+"="+resultConvert(Result)+"\n";
				MyCalcActivity.collectionData(Record, super.bLocation);
				TextViewdisplay.append(Record);
				TextViewdisplay.invalidate();
				String temp = resultConvert(Result);
				
				if(temp.contains("-"))temp = temp.replace("-", "~");
				
				editText.setText(temp);
				
			}else{
				
				Value  = post.infixToPostfix(Value);
				Result = post.result(Value);
				
				if(MyCalcActivity.bLog)if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "EqualBtn_log_postfix_Value = "+Value);
				if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "EqualBtn_log_postfix_Result = "+resultConvert(Result));
				
				Record = textFromEditText+"="+resultConvert(Result)+"\n";
				MyCalcActivity.collectionData(Record, super.bLocation);
				TextViewdisplay.append(Record);
				TextViewdisplay.invalidate();
				String temp = resultConvert(Result);
				
				if(temp.contains("-"))temp = temp.replace("-", "~");
				
				editText.setText(temp);
			}
		}
		refreshTextView(TextViewdisplay);

	}

	private String resultConvert(Double result) {
		String temp = new String();
		if(result.isInfinite()){

			temp = "Denominator is 0";

		}else if((result - result.intValue()) == 0){

			temp = String.format("%d", result.intValue());

		}else if(result.isNaN()){

			temp = "error";

		}else if(result == Double.MAX_VALUE-1){

			temp = "Out of Scale";

		}else{

			StringBuilder sbTemp = new StringBuilder(String.format("%.10f", result));

			temp = checkZero(sbTemp);
		}

		return temp;
	}
	
	private String checkZero(StringBuilder sbTemp) {
		int contentLength = sbTemp.length();
		int pointIndex = sbTemp.lastIndexOf(".");
		for (int i = contentLength; i > pointIndex; i--) {
			if (contentLength >= 1) {

				if(sbTemp.substring(i-1, i).contains("0")){
					sbTemp.deleteCharAt(i - 1);
				}else{
					break;
				}
			}
		}

		if(DisplayContentAnalyzer.isTheLastCharacterPoint(sbTemp.toString()))
			sbTemp.deleteCharAt(sbTemp.length()-1);
		
		return sbTemp.toString();
	}

	private void refreshTextView(TextView textViewdisplay) {

		textViewdisplay.postInvalidate();
		textViewdisplay.requestLayout();
		textViewdisplay.setVisibility(View.GONE);
		textViewdisplay.setVisibility(View.VISIBLE);

		if(textViewdisplay.getLineCount()>2)
			textViewdisplay.scrollBy(0, textViewdisplay.getLineHeight());

	}

	public static String changeExtendexp(String string) {
		String temp = new String();
		temp = string;
		
		if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "changeExtendexp = "+string);
		
		if(temp.contains("tan")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "tan = "+temp);
			temp = temp.replace("tan", "t");
		}
		if(temp.contains("cos")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "cos = "+temp);
			temp = temp.replace("cos", "c");
		}
		if(temp.contains("sin")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "sin = "+temp);
			temp = temp.replace("sin", "s");
		}
		if(temp.contains("log")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "log = "+temp);
			temp = temp.replace("log", "l");
		}
		if(temp.contains("fac")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "fac = "+temp);
			temp = temp.replace("fac", "f");
		}
		if(temp.contains("ln")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "ln = "+temp);
			temp = temp.replace("ln", "n");
		}
		if(temp.contains("abs")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "|x| = "+temp);
			temp = temp.replace("abs", "x");
		}
		if(temp.contains("com")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "com = "+temp);
			temp = temp.replace("com", "b");
		}
		if(temp.contains("sqrt")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "sqrt = "+temp);
			temp = temp.replace("sqrt", "q");
		}
		if(temp.contains("pow")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "pow = "+temp);
			temp = temp.replace("pow", "p");
		}
		
		if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_EQUALBUTTONLISTENER, "return = "+temp);
		
		return temp;
	}

	private int countLBracket(String temp){
		int Lcount = 0;
		
		for(int i = 0; i < temp.length(); i++){
			if(temp.charAt(i) == '('){
				Lcount++;
			}
		}
		
		return Lcount;
	}

	private int countRBracket(String temp){
		int Rcount = 0;
		
		for(int i = 0; i < temp.length(); i++){
			if(temp.charAt(i) == ')'){
				Rcount++;
			}
		}
		
		return Rcount;
	}
}

